<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/9 0009
 * Time: 下午 1:56
 */
namespace app\api\controller;

use app\admin\model\FreightDefault;
use app\admin\model\FreightSwitch;

class Freight extends Base{
    /**
     * 订单获取运费
     * @param $oproduct
     * @param $addressId
     * @return int
     */
    public function getFreight($oproduct,$addressId,$orderPrice,$request_type = 1){
        $freight = 0;
        $FreightSwitch = FreightSwitch::where('id',1)->find();
        if($FreightSwitch->mode == 1){
            //默认运费模板
            $FreightDefault = FreightDefault::where('id',1)->find();
            if($FreightDefault->f_type == 1){
                //统一运费
                $freight = $FreightDefault->unified_price;
            }else{
                if($orderPrice < $FreightDefault->full_amount){
                    $freight = $FreightDefault->s_price;
                }
            }
        }else{
            //自定义运费模板
            $allweight = 0;
            foreach ($oproduct as $v){
                $weight = \app\admin\model\Product::where('id',$v['product_id'])->value('weight');
                $allweight += $weight * $v['count'];
            }
            $Address = \app\admin\model\Address::where('id',$addressId)->field('province,city,area')->find();
            $Freight = \app\common\model\Freight::where('id','>',0)->select();
            foreach ($Freight as $v){
                $area = explode(',',$v['region']);
                foreach ($area as $val){
                    if($Address['province'] == $val){
                        if($allweight <= $v['first_weight']){
                            $freight = $v['first_weight_price'];
                        }else{
                            $chao = $allweight - $v['first_weight'];
                            if($chao >= 1000){
                                $f_price = $v['first_weight_price'] + ($chao/1000*$v['continuous_weight_price']);
                                $freight = intval($f_price);
                            }else{
                                $freight = $v['first_weight_price'];
                            }
                        }
                    }
                }
            }
        }
        if($request_type == 1){
            return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $freight);
        }else{
            return $freight;
        }



    }
}